﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using YLPublicService.Extensions;
using System.Data.Common;
using System.Data;
using Util;
using YLPublicService.TransferModel;
using System.Configuration;


namespace YLPublicService
{
    public partial class AdminService
    {

        public TemplateItem[] GetDisableTemplates(Guid guid)
        {
            return GetEnableTemplates(guid, 0);
        }

        public Process GetTemplateProcessConfig(Guid guid, int templateID)
        {
            if (isOnline(guid) == false)
            {
                return null;
            }
            var tpcs = from tpc in dbContext.TemplateProcessConfigs
                       where tpc.TemplateID == templateID
                       select tpc;
            if (tpcs.Count() > 0)
            {
                var tpc = tpcs.First();
                return Process.FromXML(tpc.ProcessDetial);
            }
            return null;

        }

        public bool UpdateTemplate(TemplateItem item, Guid guid)
        {
            if (isOnline(guid) == false)
            {
                return false;
            }
            try
            {
                var tp = from t in dbContext.TemplatesV2s
                         where t.TemplateID == Convert.ToInt32(item.mID)
                         select t;
                if (tp.Count() > 0)
                {
                    var t = tp.First();
                    t.Enable = Convert.ToInt32(item.mEnable);
                    t.AttachmentsDir = item.mAttachmentsDir;
                    t.Detial = item.mTemplateDetial.ToString();
                    dbContext.SubmitChanges();
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteTemplate(int templateID, Guid guid)
        {
            if (isOnline(guid) == false)
            {
                return false;
            }

            var tp = from t in dbContext.TemplatesV2s
                     where t.TemplateID == templateID
                     select t;
            if (tp.Count() > 0)
            {
                dbContext.TemplatesV2s.DeleteOnSubmit(tp.First());
                dbContext.SubmitChanges();
                return true;
            }
            return false;
        }
        public bool AddTemplate(TemplateItem item, Guid guid)
        {
            if (isOnline(guid) == false)
            {
                return false;
            }
            try
            {
                var templateDirName = ConfigurationManager.AppSettings["FTPServerTemplateDirName"];
                var theCreatedDir = settingManager.AddTemplateDir(onlineUserByGuid[guid].LoginName, templateDirName, item.mTemplateDetial.mName);
                TemplatesV2 t = new TemplatesV2();
                t.Enable = Convert.ToInt32(item.mEnable);
                t.AttachmentsDir = item.mAttachmentsDir;
                t.Detial = item.mTemplateDetial.ToString();
                dbContext.TemplatesV2s.InsertOnSubmit(t);
                dbContext.SubmitChanges();
                return true;
            }
            catch { return false; }
        }
    }
}